package com.yipingnai.blog.service;

import com.yipingnai.blog.mapper.ArticleMapper;
import com.yipingnai.blog.mapper.TagsMapper;
import com.yipingnai.blog.model.Article;
import com.yipingnai.blog.model.RespPageBean;
import com.yipingnai.blog.model.Tags;
import org.apache.tomcat.util.buf.UDecoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.WebServiceRef;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author: jiangym
 * @Date: 2020/4/11 13:55
 */
@Service
public class ArticleService {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    TagsMapper tagsMapper;
    @Autowired
    TagsService tagsService;

    // 文章总数
    public Integer getTotal(){
        return articleMapper.getTotal(null,null,null);
    }

    // 获取最近7天的访问量
    public List<Article> getArticleSeven(){
        return articleMapper.getArticleSeven();
    }

    // 本月发表文章数
    public Integer getCountMonth(){
        return articleMapper.getCountMonth();
    }

    // 本周发表文章数
    public Integer getCountWeek(){
        return articleMapper.getCountwWeek();
    }

    // 修改文章浏览量
    public boolean updateViewById(Integer id, Integer view){
        return articleMapper.updateViewById(id,view) > 0 ? true : false;
    }

    @Transactional
    public RespPageBean getArticle(Integer page, Integer size,String keyword) {
        page = (page - 1)*size;
        //获取文章列表
        List<Article> allArticles = articleMapper.getAllArticles(page, size,keyword);
        //获取文章总数
        Integer count = articleMapper.getTotal(null,1,keyword);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setList(allArticles);
        respPageBean.setTotal(count);
        return respPageBean;
    }

    public Article getDetail(Integer code) {
        return  articleMapper.getDetail(code);
    }

    @Transactional
    public RespPageBean getArticleByCategory(String cname, Integer page, Integer size) {
        page = (page - 1)*size;
        //获取文章列表
        List<Article> allArticles = articleMapper.getArticleByCategory(cname,page, size);
        //获取文章总数
        Integer count = articleMapper.getTotal(cname,1,null);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setList(allArticles);
        respPageBean.setTotal(count);
        return respPageBean;
    }

    public List<Article> getHotArticle() {
        return articleMapper.getHotArticle();
    }

    @Transactional
    public RespPageBean getArticleByState(Integer state,Integer page,Integer size) {
        page = (page - 1)*size;
        //获取总数
        Integer total = articleMapper.getTotal(null, state,null);
        //获取文章
        List<Article> list = articleMapper.getArticleByState(state,page,size);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(total);
        respPageBean.setList(list);
        return respPageBean;
    }

    public Integer updateArticltStateById(Integer id, Integer state) {
        return articleMapper.updateArticltStateById(id,state);
    }

    public Integer deleteArticleById(Integer id) {
        return articleMapper.deleteArticleById(id);
    }

    public Article getArticleById(Integer id) {
        return articleMapper.getArticleById(id);
    }

    @Transactional
    public int addArticle(Article article) {
        //处理文章摘要
        if (article.getSummary() == null || "".equals(article.getSummary())) {
            //直接截取
            String stripHtml = stripHtml(article.getHtmlContent());
            article.setSummary(stripHtml.substring(0, stripHtml.length() > 50 ? 50 : stripHtml.length()));
        }
        //判断id 如何是-1就是添加
        if(article.getId() == -1){
            //插入分两步1.文章2.标签
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            article.setPublishDate(timestamp);//设置发布日期
            article.setEditTime(timestamp);//设置编辑日期
            int insert = articleMapper.addArticle(article);//设置主键id返回
            //更新标签
            String[] dynamicTags = article.getDynamicTags();
            if(dynamicTags.length>0 && dynamicTags != null){
                //更新标签
                int updateTag = updateArticleTags(article.getId(),dynamicTags);
            }
            //返回id
            return insert == 1 ? 1 : 0;
        }else{
            if (article.getSummary() == null || "".equals(article.getSummary())) {
                //直接截取
                String stripHtml = stripHtml(article.getHtmlContent());
                article.setSummary(stripHtml.substring(0, stripHtml.length() > 50 ? 50 : stripHtml.length()));
            }
            //修改文章
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            article.setEditTime(timestamp);
            int update = articleMapper.updateArticle(article);
            //修改标签
            String[] dynamicTags = article.getDynamicTags();
            //有标签就更新
            if(dynamicTags.length>0 && dynamicTags != null){
                //更新标签
                int updateTag = updateArticleTags(article.getId(),dynamicTags);
            }else {
                //没有标签就去直接删除 避免因为本来就有，编辑文章就删除了标签的情况
                tagsMapper.deleteArticleTid(article.getId().intValue());
            }
            //更新
            return update == 1 ? 1 : 0;
        }
    }

    @Transactional
    public int updateArticleTags(Long id, String[] dynamicTags) {
        //如果是更新文章标签的话标签数组为空的话，直接删除文章标签，根据状态判断
        //
        //添加标签
        List<Tags> tags = tagsService.addTags(dynamicTags);//返回添加的标签对象集合
        //删除文章相关的tagid
        articleMapper.deleteTags(id);
        //更新文章中间表
        int result = tagsMapper.addTags(tags,id);
        System.out.println(result);
        return result == dynamicTags.length ? 1:0;
    }
    public String stripHtml(String content) {
        content = content.replaceAll("<p .*?>", "");
        content = content.replaceAll("<br\\s*/?>", "");
        content = content.replaceAll("\\<.*?>", "");
        return content;
    }


}
